//
//  CZVisitorView.swift
//  XLWB009
//
//  Created by apple on 3/22/16.
//  Copyright © 2016 apple. All rights reserved.
//

import UIKit

@objc protocol CZVisitorViewDelegate:NSObjectProtocol{

    //定义方法
    //注册按钮
    optional func visitorViewDidClickReterButton()
    
    //点击按钮
    optional func visitorViewDidClickLoginButton()
}

class CZVisitorView: UIView {
    
    //定义代理属性 ,用weak防止发生循环引用
   weak var delegate:CZVisitorViewDelegate?
    
    //访客视图
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置不同面页不同信息
    func setUpVisitorinfo(imageName:String,messge:String){
        iconView.image=UIImage(named:imageName)
        
        lableView.text=messge
        lableView.sizeToFit()
        
        //隐藏小房子
        HomeView.hidden=true
        
        sendSubviewToBack(messView)
        
    }
    
    //旋转动画
    func startRotationAnimation(){
        let animation=CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue=M_PI*2
        animation.duration=20
        animation.repeatCount=MAXFLOAT
        
        //发现切换到其他tabar在切换回来就停止转动，需加上这句
        animation.removedOnCompletion=false
        
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    //懒加载 转轮
    private lazy var iconView:UIImageView={
        
        let image=UIImage(named:"visitordiscover_feed_image_smallicon")
        
        return UIImageView(image: image)
    }()
    
    //小房子
    private lazy var HomeView:UIImageView={
        
        let image=UIImage(named: "visitordiscover_feed_image_house")
        
        return UIImageView(image: image)
        
    }()
    
    //遮罩
    private lazy var messView:UIImageView={
        
        let image=UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return UIImageView(image: image)
    }()
    
    //文本文字
    private lazy var lableView:UILabel={
    
        let lable=UILabel()
        lable.text="关注一些人,回这里看看有上面惊喜"
        lable.font=UIFont.systemFontOfSize(16)
        lable.textAlignment = NSTextAlignment.Center
        lable.sizeToFit()
        lable.numberOfLines=0
        
        return lable
    }()
    
    //注册
    private lazy var registerButton:UIButton={
        
        let registerButton=UIButton()
        registerButton.setTitle("注册", forState: UIControlState.Normal)
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registerButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //点击事件
        registerButton.addTarget(self, action: "didClickReterButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return registerButton
    }()
    
    //登陆
    private lazy var loginButton:UIButton={
    
        let loginButton=UIButton()
        loginButton.setTitle("登陆", forState: UIControlState.Normal)
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        //点击事件
        loginButton.addTarget(self, action: "didClickLoginButton:", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginButton
    }()
    
    //按钮点击事件
    @objc func didClickReterButton(button:UIButton){
        //调用代理方法
     // visitorViewDidClickRegisterButton?:表示代理实现了这个方法就调用这个方法，没有实现就不调用
        delegate?.visitorViewDidClickReterButton?()
//        print("-------")
        
    }
    
    func didClickLoginButton(button:UIButton){
    
//        print("-------")
        //调用代理的方法
        delegate?.visitorViewDidClickLoginButton?()
    }
    
    //添加控件
    func prepareUI(){
        //设置背景颜色
        backgroundColor=UIColor(white: 237/255.0, alpha: 1)
        
        //设置约束 转轮
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //小房子
        addConstraint(NSLayoutConstraint(item: HomeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: HomeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute:NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //遮罩
        addConstraint(NSLayoutConstraint(item: messView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem:iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //文字说明
        addConstraint(NSLayoutConstraint(item: lableView, attribute:NSLayoutAttribute.Top, relatedBy:NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: lableView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:300))
        addConstraint(NSLayoutConstraint(item: lableView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0))
        
        //注册
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: lableView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem:lableView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 40))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute:NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登陆
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: lableView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -40))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        //约束
        iconView.translatesAutoresizingMaskIntoConstraints=false
        HomeView.translatesAutoresizingMaskIntoConstraints=false
        lableView.translatesAutoresizingMaskIntoConstraints=false
        registerButton.translatesAutoresizingMaskIntoConstraints=false
        loginButton.translatesAutoresizingMaskIntoConstraints=false
        messView.translatesAutoresizingMaskIntoConstraints=false
        addSubview(iconView)
        addSubview(HomeView)
        addSubview(lableView)
        addSubview(registerButton)
        addSubview(loginButton)
        addSubview(messView)
    }
}
